//
//  KYLeftVc.m
//  KYDemo
//
//  Created by 李岚清 on 16/5/21.
//  Copyright © 2016年 李岚清. All rights reserved.
//

#import "NQLeftMenuVc.h"
#import "ICSDrawerController.h"
#import "NQLeftMenuCell.h"
#import "NQLeftMenuHeaderView.h"
#import "NQDrawerManager.h"
#import "NQUserInfo.h"
//友盟统计
#import <UMMobClick/MobClick.h>
//cell重用标识
static NSString *NQLeftMenuVcIdentifier = @"NQLeftMenuVcIdentifier";

@interface NQLeftMenuVc ()
/**
 *  菜单头部视图 -- 个人信息
 */
@property (nonatomic, strong) NQLeftMenuHeaderView *MenuHeaderView;
/**
 *  数据源数组 -- 个人信息数组
 */
@property (nonatomic, strong) NSArray *infoArr;

/**
 *  用于覆盖的view
 */
@property (nonatomic, strong) UIView *shadowView;

@end

@implementation NQLeftMenuVc
#pragma mark - set/get
- (void)setUserInfo:(NQUserInfo *)userInfo{
    _userInfo = userInfo;
    //赋值
    self.MenuHeaderView.iconView.image = userInfo.iconImage;
    self.MenuHeaderView.nameLabel.text = userInfo.name;
}

#pragma mark - 重写init方法
- (instancetype)init{
    if (self = [super init]) {
        self = [super initWithStyle:UITableViewStyleGrouped];
    }
    return self;
}

#pragma mark - 控制器View的生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    //关闭滚动
    self.tableView.scrollEnabled = NO;
    
    //设置侧页面背景色
    self.view.backgroundColor = NQColor(240, 238, 246);
    //注册cell
    [self.tableView registerClass:[NQLeftMenuCell class] forCellReuseIdentifier:NQLeftMenuVcIdentifier];
    
    //解档
    NQUserInfo *userInfo = [NQUserInfo readUserInfo];
    if (userInfo) {
        self.MenuHeaderView.iconView.image = userInfo.iconImage;
        self.MenuHeaderView.nameLabel.text = userInfo.name;
    }
    //设置头部视图
    self.tableView.tableHeaderView = self.MenuHeaderView;
}

//友盟统计
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:NSStringFromClass([self class])];//("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:NSStringFromClass([self class])];
}

#pragma mark - UITableView数据源方法

//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

//每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

//单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //1.创建单元格
    NQLeftMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:NQLeftMenuVcIdentifier];
    
    //2.赋值cell
    cell.title = self.infoArr[indexPath.section][indexPath.row];
    
    //3.返回单元格
    return cell;
}

#pragma mark - UITableView代理方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

//选中了某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 这里加的这个条件是,让点击分享的时候不会跳转回主界面
    if (!(indexPath.section == 1 & indexPath.row == 2)) {
        
        //先设置关闭状态
        ICSDrawerController *icsVc = (ICSDrawerController *)NQKeyWindow.rootViewController;
        [icsVc close];
    }
    //调用代理方法,把点击cell的indexPath传过去
    if ([self.delegate respondsToSelector:@selector(leftMenuVcDidClickCell:)]) {
        [self.delegate leftMenuVcDidClickCell:indexPath];
    }

}

#pragma mark - ICSDrawerControllerPresenting

- (void)drawerControllerWillOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
    
}

- (void)drawerControllerDidOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
    
    self.shadowView = [[UIView alloc] initWithFrame:NQDrawer.centerViewController.view.frame];
    [NQDrawer.centerViewController.view addSubview:self.shadowView];
}

- (void)drawerControllerWillClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

- (void)drawerControllerDidClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;

    [self.shadowView removeFromSuperview];
}

#pragma mark - 懒加载
- (NQLeftMenuHeaderView *)MenuHeaderView{
    if (_MenuHeaderView == nil) {
        _MenuHeaderView = [[NQLeftMenuHeaderView alloc] init];
        
        _MenuHeaderView.backgroundColor = NQColor(50, 190, 190);
        _MenuHeaderView.frame = CGRectMake(0, 0, 0, 100);
    }
    return _MenuHeaderView;
}

- (NSArray *)infoArr{
    if (_infoArr == nil) {
        _infoArr = @[@[@"名医通申请",@"病例管理",@"关注医生"],@[@"设置与帮助",@"关于产品",@"分享给好友"]];
    }
    return _infoArr;
}

@end
